<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>今天吃什么</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            text-align: center;
            background-color: #f5f5f5;
        }
        h1 {
            color: #ff6b6b;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        #result {
            margin: 20px 0;
            font-size: 24px;
            min-height: 50px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 5px;
        }
        button {
            background-color: #ff6b6b;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #ff5252;
        }
        .loading {
            display: none;
            margin: 20px 0;
        }
        .error {
            color: red;
            margin: 20px 0;
        }
        .fallback-hint {
            color: orange;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>今天吃什么</h1>
        <p>不知道今天吃什么？让AI帮你决定！</p>
        
        <button id="recommendBtn">给我推荐</button>
        
        <div class="loading" id="loading">
            <p>正在思考中...</p>
        </div>
        
        <div id="result"></div>
        <div class="error" id="error"></div>
    </div>

    <script>
        document.getElementById('recommendBtn').addEventListener('click', function() {
            const resultDiv = document.getElementById('result');
            const errorDiv = document.getElementById('error');
            const loadingDiv = document.getElementById('loading');
            
            resultDiv.innerHTML = '';
            errorDiv.innerHTML = '';
            loadingDiv.style.display = 'block';
            
            fetch('/recommend')
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP错误! 状态码: ${response.status}`);
                    }
                    return response.json();
                }) 
                .then(data => {
                    loadingDiv.style.display = 'none';
                    
                    if (data.success && data.recommendations?.length > 0) {
                        const food = data.recommendations[0];
                        resultDiv.innerHTML = `
                            <h2>推荐结果</h2>
                            <p>今天你可以吃：<strong>${food}</strong></p>
                        `;
                    } else {

                        const fallbackFood = data.recommendations?.[0] || '随机选择';
                        resultDiv.innerHTML = `
                            <h2>推荐结果</h2>
                            <p>今天你可以吃：<strong>${fallbackFood}</strong></p>
                            <p class="fallback-hint">(备用推荐)</p>
                        `;
                        
                        if (data.message) {
                            errorDiv.textContent = `API错误: ${data.message}`;
                        }
                    }
                })
                .catch(error => {
                    loadingDiv.style.display = 'none';
                    errorDiv.textContent = `请求失败: ${error.message}`;
                    resultDiv.innerHTML = `
                        <h2>推荐结果</h2>
                        <p>今天你可以吃：<strong>${['自选快餐', '麻辣烫', '烧烤'][Math.floor(Math.random() * 3)]}</strong></p>
                        <p class="fallback-hint">(本地随机推荐)</p>
                    `;
                });
        });
    </script>
</body>
</html>